package com.summer.urlmapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.summer.core.util.SpringBeanUtil;
import com.summer.urlmapping.model.MetaMapping;

@Component
@Scope
public class UrlMappingUtil {
	
	private HibernateTemplate ht;
		
//	@Logger
//	private static Log log;
	
	Map<String,MetaMapping> map = new HashMap<String,MetaMapping>();
	
	public UrlMappingUtil(){
		//初始化
		if(map==null||map.size()==0){
			ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
			List<MetaMapping> lu = ht.find(" from MetaMapping");
			for(MetaMapping u:lu){
				map.put(u.getViewId(), u);
			}
		}
	}
	
	/**
	 * 请求路径转换
	 * @param ServletPatn
	 * @return
	 */
	private String realPath(String ServletPatn){
		return ServletPatn.replace("/site/themes/default/templates/", "themepath:");
	}
		
	private MetaMapping getMap(String servletPath){
		MetaMapping u = map.get(realPath(servletPath));
		if(u==null){
			u = new MetaMapping();
		}
		return u;
	}
	/**
	 * 获取title
	 * @param ServletPath
	 * @return
	 */
//	public String getTitle(String servletPath){
//		return getMap(servletPath).getTitle()!=null?map.get(realPath(servletPath)).getTitle():"玺欢钱-安全的金融服务机构、安全的互联网理财平台_xihuanqian.com";
//	}
//	
//	public String getTitle(HttpServletRequest request){
//		String servletPath = request.getServletPath();//p2p/loanDetail
//		if(servletPath.indexOf("p2p/loanDetail")>=0){
//			String loanid = request.getParameter("loanId");
//			if(ht==null){
//				ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
//			}
//			Loan loan = ht.get(Loan.class, loanid);
//			
//			return loan.getName()+"-玺欢钱";
//		}
//		return getTitle(servletPath);
//		
//	}
	/**
	 * 获取keywords
	 * @param servletPath
	 * @return
	 */
//	public String getKeywords(String servletPath){
//		return getMap(servletPath).getKeywords()!=null?map.get(realPath(servletPath)).getKeywords():"玺欢钱,网络理财,个人理财,投资理财,P2P理财,互联网金融,投资理财,债权转让,理财计划,优选计划,网络贷款,工薪贷,生意贷,小额贷款,无抵押贷款,个人无抵押贷款,个人贷款,汽车贷款";
//	}
//	public String getKeywords(HttpServletRequest request){
//		String servletPath = request.getServletPath();//p2p/loanDetail
//		if(servletPath.indexOf("p2p/loanDetail")>=0){
//			String loanid = request.getParameter("loanId");
//			if(ht==null){
//				ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
//			}
//			Loan loan = ht.get(Loan.class, loanid);
//			
//			return loan.getName()+",玺欢钱";
//		}
//		return getKeywords(servletPath);
//	}
	/**
	 * 获取description
	 * @param servletPath
	 * @return
	 */
//	public String getDescription(String servletPath){
//		return getMap(servletPath).getDescription()!=null?map.get(realPath(servletPath)).getDescription():"玺欢钱（www.xihuanqian.com）系华玺旗下的互联网金融交易平台，喜欢钱吗?请上'玺欢钱'!'安全便捷我喜欢'!自上线以来，玺欢钱以其诚信、透明、安全、开放、便捷、创新的特性赢得了良好的用户口碑。玺欢钱理财平台,致力打造中国最安全的金融服务机构,为投资者提供玺洋洋计划、满季鑫、双季赢等优质理财产品,随时转让,资金灵活!";
//	}
//	public String getDescription(HttpServletRequest request){
//		String servletPath = request.getServletPath();//p2p/loanDetail
//		if(servletPath.indexOf("p2p/loanDetail")>=0){
//			String loanid = request.getParameter("loanId");
//			if(ht==null){
//				ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
//			}
//			Loan loan = ht.get(Loan.class, loanid);
//			
//			return "玺欢钱"+loan.getName()+"借款详情页";
//		}
//		return getDescription(servletPath);
//	}
	/**
	 * 获取renderer
	 * @param servletPath
	 * @return
	 */
	public String getRenderer(String servletPath){
		return getMap(servletPath).getRenderer()!=null?map.get(realPath(servletPath)).getRenderer():"webkit";
	}
	public String getRenderer(HttpServletRequest request){
		String servletPath = request.getServletPath();//p2p/loanDetail
//		if(servletPath.indexOf("p2p/loanDetail")>=0){
//			String loanid = request.getParameter("loanId");
//			if(ht==null){
//				ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
//			}
//			Loan loan = ht.get(Loan.class, loanid);
//			
//			return "webkit";
//		}
		return getRenderer(servletPath);
	}
	/**
	 * 重置
	 */
	public void reset(){
		map.clear();
		ht = (HibernateTemplate) SpringBeanUtil.getBeanByName("ht");
		List<MetaMapping> lu = ht.find(" from MetaMapping");
		for(MetaMapping u:lu){
			map.put(u.getViewId(), u);
		}
	}
}
